package com.xudow.app.newui;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.user.InspectionTeam;
import com.makeramen.roundedimageview.RoundedImageView;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.model.Update;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.newui.task.InspectionTeamTask;
import com.xudow.app.util.ImageUtils;

import java.util.List;

import butterknife.BindView;
import it.sephiroth.android.library.widget.AdapterView;
import it.sephiroth.android.library.widget.HListView;

/**
 * 督导团队详情
 */
public class InspectionTeamActivity extends BaseUiActivity implements AdapterView.OnItemClickListener {

    @BindView(R.id.team_list)
    HListView team_list;

    @BindView(R.id.head_ico)
    RoundedImageView head_ico;

    @BindView(R.id.content_name)
    TextView contentname;

    @BindView(R.id.course_count_tv)
    TextView check_count;

    @BindView(R.id.personal_inrodu)
    TextView sefintroduce;
    TeamAdapter myadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inspection_team);
        myadapter=new TeamAdapter(this);
        team_list.setAdapter(myadapter);

    }

    @Override
    protected void initData() {
        InspectionTeamTask task=new InspectionTeamTask(this,getTeamDataHandler);
        addTask(task);
        task.execute();
        showLodingDialog("正在加载");
    }
    private List<InspectionTeam> teams;
    private Handler getTeamDataHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            hideLoadingDialog();
            if (msg.what==InspectionTeamTask.SUCCESS){
                teams=(List<InspectionTeam>)msg.obj;
                Log.e("teamsize:",teams.size()+"" );
                if (null!=teams&&teams.size()>0){
                    myadapter.setData(teams);
                    team_list.performItemClick(team_list.getChildAt(0),0,team_list.getItemIdAtPosition(0));
                }
            }else
                getMyApp().makeToast(msg.getData().getString("errMessage"));
        }
    };
    public void onBackBtnClick(View view) {
        finish();
    }

    @Override
    protected void initListener() {
       team_list.setOnItemClickListener(this);
        //mListView.performItemClick(mListView.getChildAt(your_click_position), your_click_position, mListView.getItemIdAtPosition(your_click_position));

    }





    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        cleanAllselect();
        teams.get(i).isSelect=true;
        myadapter.notifyDataSetChanged();
        InspectionTeam team=teams.get(i);
        UpdateTeamInfo(team);
    }

    private void UpdateTeamInfo(InspectionTeam team) {
        contentname.setText(team.getFullname());
        sefintroduce.setText(team.getIntroduce());
        long count=0;
        if (null!=team.getCheckCourseNum())
            count=team.getCheckCourseNum();
        check_count.setText(count+"");
        //String url = String.format("%s%s?name=%s", Config.BASE_URL, Config.IMAGE_LOAD_URL, team.getImgList().get(0).getImgPath());
        if (null!=team.getImgList()&&team.getImgList().size()>0) {
            ImageUtils.loadImage(this, head_ico, team.getImgList().get(0).getImgPath(), R.mipmap.def_user_ico);
            Log.e("imgurl:",team.getImgList().get(0).getImgPath());
        }else
            ImageUtils.loadImage(this, head_ico, null, R.mipmap.def_user_ico);

        head_ico.setCornerRadius((float)5);
    }

    private void cleanAllselect() {
        for (InspectionTeam team:teams){
            team.isSelect=false;
        }
        myadapter.notifyDataSetChanged();
    }

    class TeamAdapter extends BaseAdapter{
        Context cnt;
        List<InspectionTeam> data;

        public TeamAdapter(Context cnt) {
            this.cnt = cnt;
        }

        public void setData(List<InspectionTeam> data){
            this.data=data;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            if (null==data||data.size()==0)
                return 0;
            else
                return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            ViewHolder holder;
            if (null==v){
                holder=new ViewHolder();
                v=View.inflate(cnt,R.layout.item_team_list,null);
                holder.name=(TextView)v.findViewById(R.id.name);
                holder.bottom=(ImageView)v.findViewById(R.id.bottom);
                v.setTag(holder);
            }else
                holder=(ViewHolder)v.getTag();
                InspectionTeam team=data.get(position);
            holder.name.setText(team.getFullname());
            if (team.isSelect){
                holder.name.setTextColor(Color.parseColor("#068600"));
                holder.bottom.setVisibility(View.VISIBLE);
            }else {
                holder.name.setTextColor(Color.parseColor("#666666"));
                holder.bottom.setVisibility(View.INVISIBLE);
            }


            return v;
        }

        class ViewHolder {
            ImageView bottom;
            TextView name;
        }
    }
}
